package com.jz.community.basecomm.utils;

import android.graphics.Color;

/**
 * Created by Tony.Fan on 2019/1/15 14:45
 */
public class ColorUtils {


    public static String parseStyleColor(String bannerColor) throws Exception {

        String color = AgexUtils.filtration(bannerColor);

        String color1 = color.substring(0, 2);
        String color2 = color.substring(2, 4);
        String color3 = color.substring(4, 6);

        int indexCol = (int) ((int) Long.parseLong(color1, 16) * 0.8f);
        int indexCo2 = (int) ((int) Long.parseLong(color2, 16) * 0.8f);
        int indexCo3 = (int) ((int) Long.parseLong(color3, 16) * 0.8f);
        String strHex;
        String strHex1;
        String strHex2;

        if (indexCol == 0) {
            strHex = Integer.toHexString(indexCol) + "0";
        } else {
            strHex = Integer.toHexString(indexCol);
        }

        if (indexCo2 == 0) {
            strHex1 = Integer.toHexString(indexCo2) + "0";
        } else {
            strHex1 = Integer.toHexString(indexCo2);
        }

        if (indexCo3 == 0) {
            strHex2 = Integer.toHexString(indexCo3) + "0";
        } else {
            strHex2 = Integer.toHexString(indexCo3);
        }

        return "#" + strHex + strHex1 + strHex2;

    }

    public static int parseBackgroundColor2(int color) {
        int counter = 0;
        counter += Color.red(color) >= 128 ? 1 : 0;
        counter += Color.green(color) >= 128 ? 1 : 0;
        counter += Color.blue(color) >= 128 ? 1 : 0;
        return counter >= 2 ? Color.BLACK : Color.WHITE;
    }

    // 通过分析背景色来决定当前文字的匹配颜色，使文字颜色自适应背景颜色
    public static int parseBackgroundColor(int color) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        if (red >= 128 && green >= 128      // 三选二
                || red >= 128 && blue >= 128
                || green >= 128 && blue >= 128) {
            return Color.rgb(0, 0, 0);
        }
        return Color.rgb(255, 255, 255);
    }

    // #FF55FF => color
    // int color = Color.parseColor("#b64242");

    // color -> #FF55FF
    public static String toRGBHexString(final int color) {
        return toRGBHexString(Color.red(color), Color.green(color), Color.blue(color));
    }

    // (r,g,b) -> #FF55FF
    public static String toRGBHexString(int red, int green, int blue) {
        return toARGBHexString(-1, red, green, blue);
    }

    // default prefix: "#"
    // (a,r,g,b) -> #FF55FF55
    public static String toARGBHexString(int alpha, int red, int green, int blue) {
        return toARGBHexString("#", alpha, red, green, blue);
    }

    public static String toARGBHexString(String prefix, int alpha, int red, int green, int blue) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        if (alpha != -1) {
            String mAlphaStr = Integer.toHexString(alpha);
            sb.append(mAlphaStr.length() == 1 ? "0" + mAlphaStr : mAlphaStr);
        }
        String mRedStr = Integer.toHexString(red);
        sb.append(mRedStr.length() == 1 ? "0" + mRedStr : mRedStr);
        String mGreenStr = Integer.toHexString(green);
        sb.append(mGreenStr.length() == 1 ? "0" + mGreenStr : mGreenStr);
        String mBlueStr = Integer.toHexString(blue);
        sb.append(mBlueStr.length() == 1 ? "0" + mBlueStr : mBlueStr);
        return sb.toString().toUpperCase();
    }

}
